﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace TauschFormat2
{
	/// <summary>
	/// Interaktionslogik für BBCode.xaml
	/// </summary>
	public partial class BBCode : Window
	{
        BBCodeDataSet bbData = new BBCodeDataSet();
        BBCodeDataSetTableAdapters.BBCodesTableAdapter bbDataAdapter = new BBCodeDataSetTableAdapters.BBCodesTableAdapter();
        String oldname = "";
		public BBCode()
		{
			this.InitializeComponent();
			
			// Fügen Sie Code, der bei der Objekterstellung erforderlich ist, unter diesem Punkt ein.
		}

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loaddata();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (lbCodes.SelectedItem == null)
            {
                MessageBox.Show("Keinen Code ausgewählt", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            bbDataAdapter.Fill(bbData.BBCodes);
            DataRow[] codeRow = bbData.BBCodes.Select("name = '" + oldname + "'");

            codeRow[0]["name"] = txtName.Text;
            codeRow[0]["expression"] = txtExpression.Text;
            codeRow[0]["replace"] = txtReplace.Text;

            bbDataAdapter.Update(bbData.BBCodes);
            loaddata();
        }

        private void lbCodes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbCodes.SelectedItem == null)
            {
                return;
            }
            bbDataAdapter.Fill(bbData.BBCodes);
            var x = from r in bbData.BBCodes where r.name == lbCodes.SelectedItem.ToString() select r;
            oldname = lbCodes.SelectedItem.ToString();
            foreach (DataRow r in x)
            {
                txtExpression.Text = (string)r["expression"];
                txtReplace.Text = (string)r["replace"];
                txtName.Text = (string)r["name"];
                break;
            }
        }
        private void loaddata()
        {
            lbCodes.Items.Clear();
            bbDataAdapter.Fill(bbData.BBCodes);
            var x = from r in bbData.BBCodes select r.name;
            foreach (string s in x)
            {
                lbCodes.Items.Add(s);
            }
        }

        private void bttnNeuerCode_Click(object sender, RoutedEventArgs e)
        {
            bbData.BBCodes.AddBBCodesRow("", "", "Neuer BBCode");
            bbDataAdapter.Update(bbData.BBCodes);
            loaddata();
        }

        private void bttnDeleteBBC_Click(object sender, RoutedEventArgs e)
        {
            if (lbCodes.SelectedItem == null)
            {
                MessageBox.Show("Keinen Style ausgewählt", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            bbDataAdapter.Delete(lbCodes.SelectedItem.ToString());
            loaddata();
        }

	}
}